package com.example.controller;


import com.example.dao.AgencyCrudRepository;
import com.example.pojo.Agency;
import com.example.pojo.PageBean;
import com.example.pojo.User;
import com.example.tools.JwtUtils;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/agency")
public class AgencyController {
    @Autowired
    private AgencyCrudRepository agencyCrudRepository;


    @PostMapping("/addAgency")
    public Result add(@RequestBody Agency agency) {
        log.info("addAgency:{}", agency);
        agencyCrudRepository.save(agency);
        return Result.success(agency);
    }


    @DeleteMapping("/deleteAgency")
    public Result delete(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            agencyCrudRepository.deleteById(id);
        }
        return Result.success("删除成功");
    }

    @PostMapping("/updateAgency")
    public Result update(@RequestBody Agency agency) {
        log.info("updateagency:{}", agency);
        agencyCrudRepository.save(agency);
        return Result.success(agency);
    }

    //条件查询
    @PostMapping("/getAllAgency")
    public Result getAllAgency(@RequestBody Agency agency,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("agency:{}",agency);
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.Direction.ASC, "id");

        Page<Agency> coursePage = agencyCrudRepository.findAll(new Specification<Agency>() {
            @Override
            public Predicate toPredicate(Root<Agency> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取条件对象
                Predicate predicate = cb.conjunction();
                //判断用户对象
                if (agency != null) {
                    // 根据 student 的属性添加查询条件
                    if (agency.getName() != null) {
                        predicate.getExpressions().add(cb.like(root.get("name"), "%" + agency.getName() + "%"));
                    }
                    if (agency.getId() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("id"), agency.getId()));
                    }
                    if (agency.getType() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("type"), agency.getType()));
                    }
                }
                return predicate;
            }
        }, pageable);
        List<Agency> agencys = coursePage.getContent();
        long elements = coursePage.getTotalElements();
        return Result.success(new PageBean(elements, agencys));
    }

    @GetMapping("/getAgencyList")
    public Result getAgencyList() {
        Iterable<Agency> all = agencyCrudRepository.findAll();
        List<Agency> agencyList = new ArrayList<>();
        for (Agency agency : all) {
            agencyList.add(agency);
        }
        return Result.success(agencyList);
    }
}
